package com.example.attribute_animation;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button btnStrat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStrat = findViewById(R.id.btn_start);

        btnStrat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //startValueAnimatorJava();
                //startValueAnimatorXML();
                startValueAnimatorWithJava();

            }
        });
    }

    private void startValueAnimatorWithJava() {
        Point pointStart = new Point(100f,100f);
        Point pointEnd = new Point(500f,500f);

        ValueAnimator valueAnimator = ValueAnimator.ofObject(new FloatEvaluator(), pointStart,pointEnd);
        valueAnimator.setDuration(3000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Point currentPoint = (Point) animation.getAnimatedValue();
                int width = Float.valueOf(currentPoint.getX()).intValue();
                int heigh = Float.valueOf(currentPoint.getY()).intValue();


                btnStrat.getLayoutParams().width =  width;
                btnStrat.getLayoutParams().height = heigh;
                btnStrat.requestLayout();
            }
        });

        valueAnimator.start();


    }

    private void startValueAnimatorXML() {
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(
                MainActivity.this, R.animator.set_animation);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                btnStrat.setBackgroundColor(Color.argb(255,value/2,0,0));
                btnStrat.requestLayout();
            }
        });

        valueAnimator.start();
    }

    private void startValueAnimatorJava() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(btnStrat.getLayoutParams().width);
        valueAnimator.setDuration(3000);
        valueAnimator.setStartDelay(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int width = (int) animation.getAnimatedValue();
                btnStrat.getLayoutParams().width = width;
                btnStrat.requestLayout();
            }
        });

        valueAnimator.start();
    }
}
